package com.hxj.common.core.pojo.bo;


import com.hxj.common.core.enums.ResultStatus;
import lombok.Data;

import java.io.Serializable;

/**
 * @Author txf
 * @Date 2023/11/13 14:56
 * @Description 返回结果类
 */
@Data
public class RestResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 消息
     */
    private String message;

    /**
     * 数据
     */
    private T data;

    /**
     * 返回成功
     * @return
     */
    public static <T> RestResult<T> success() {
        return returnRestResult(ResultStatus.SUCCESS.getCode(), ResultStatus.SUCCESS.getMessage(), null);
    }

    /**
     * 传入数据返回成功
     * @param data
     * @return
     * @param <T>
     */
    public static <T> RestResult<T> success(T data) {
        return returnRestResult(ResultStatus.SUCCESS.getCode(), ResultStatus.SUCCESS.getMessage(), data);
    }

    /**
     * 返回失败
     * @return
     */
    public static <T> RestResult<T> fail() {
        return returnRestResult(ResultStatus.SERVER_ERROR.getCode(), ResultStatus.SERVER_ERROR.getMessage(), null);
    }

    /**
     * 传入状态枚举对象返回失败
     * @return
     */
    public static <T> RestResult<T> fail(ResultStatus resultStatus) {
        return returnRestResult(resultStatus.getCode(), resultStatus.getMessage(), null);
    }

    /**
     * 传入消息返回失败
     * @return
     */
    public static <T> RestResult<T> fail(String message) {
        return returnRestResult(ResultStatus.SERVER_ERROR.getCode(), message, null);
    }

    /**
     * 传入消息返回失败
     * @return
     */
    public static <T> RestResult<T> fail(Integer statusCode, String message, T data) {
        return returnRestResult(statusCode, message, data);
    }

    /**
     * 传入状态码和消息返回失败
     * @return
     */
    public static <T> RestResult<T> fail(Integer statusCode, String message) {
        return returnRestResult(statusCode, message, null);
    }

    private static <T> RestResult<T> returnRestResult(Integer code, String message, T data) {
        RestResult<T> restResult = new RestResult<T>();
        restResult.setCode(code);
        restResult.setMessage(message);
        restResult.setData(data);
        return restResult;
    }

}
